package org.brisling.workflow.queryService;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.activiti.engine.task.Task;
import org.brisling.authorize.shiro.ShiroSecurityService;
import org.brisling.base.domain.ListDomain;
import org.brisling.base.service.AbstractBaseQueryService;
import org.brisling.common.ConstSysParam;
import org.brisling.common.exception.DangerousOperatorException;
import org.brisling.sysman.queryService.QueryDictionaryService;
import org.brisling.workflow.domain.VOFlowCount;
import org.brisling.workflow.domain.VOFlowDomain;
import org.brisling.workflow.domain.WorkflowDomain;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/workFlow/query")
public class QueryWorkFlowService extends AbstractBaseQueryService<WorkflowDomain>{
	
	private static final String flow_biz_view = ConstSysParam.VIEW_WORKFLOW_BIZ_INFO;
	
	@Autowired
	private	ShiroSecurityService shiroSecurityService;
	@Autowired
	private QueryDictionaryService dicService;
	public QueryWorkFlowService(){		
		
		this.clazz = WorkflowDomain.class;
		typeMap = this.getTypeMap(clazz);	
		
		orderMap.put("sortedid","");			
		
	}
	
	
	
	@PersistenceContext(unitName=ConstSysParam.SUBSYSTEM_SYSMAN)
	@Autowired
	protected void setEntityManager(EntityManager entityManager){
		this.entityManager = entityManager;		
	}
	
	
	@RequestMapping("/flowHistoricsListDomain.action")
	public ListDomain<Object> getHistoricByProcInstId(@RequestBody(required=false) String body){
		
		String sql = dicService.getSqlByBizcode("flow_historics");				
		return super.getHistoricByProcInstId(body, sql);
	}
	
	/**
	 * 获取当前用户待办、在办任务
	 * @param body
	 * @return
	 */
	@RequestMapping("/flowOperateListDomain.action")
	public ListDomain<VOFlowDomain> flowOperateListDomain(
				@RequestParam(value="userid",required=false) String userid,
				@RequestParam(value="page",required=false) Integer page,
				@RequestParam(value="rows",required=false) Integer rows
			){
		
		String sql = dicService.getSqlByBizcode("flow_user_operate_list");
		userid =userid==null? shiroSecurityService.getCurrentUserIdentify():userid;
		sql = String.format(sql, userid,flow_biz_view);
		Integer _page = page==null?ConstSysParam.PAGE:page;
		Integer _row = rows==null?ConstSysParam.ROWS:rows;
		ListDomain<VOFlowDomain> lists = super.getPagedListDomain(_page, _row, sql,VOFlowDomain.class);
		return lists;
	}
	
	/**
	 * 获取当前用户待办、在办任务数量
	 * @param userid	用户编码，为空取当前登录用户	 
	 * @return
	 */
	@RequestMapping("/flowOperateCount.action")
	public List<VOFlowCount> flowOperateCount(
				@RequestParam(value="userid",required=false) String userid
			){
		
		String sql = dicService.getSqlByBizcode("flow_user_operate_count");
		userid =userid==null? shiroSecurityService.getCurrentUserIdentify():userid;
		sql = String.format(sql, userid,flow_biz_view);
		
		List<VOFlowCount> lists = null;
		try {
			lists = super.getClassedAllList(sql, VOFlowCount.class);
		} catch (DangerousOperatorException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		
		return lists;
	}
	
	/**
	 * 获取当前用户待办、在办任务数量
	 * @param userid	用户编码，为空取当前登录用户	 
	 * @return
	 */
	@RequestMapping("/flowOperateAllCount.action")
	public List<VOFlowCount> flowOperateAllCount(
				@RequestParam(value="userid",required=false) String userid
			){
		
		String sql = dicService.getSqlByBizcode("flow_user_operate_count");
		userid =userid==null? shiroSecurityService.getCurrentUserIdentify():userid;
		sql = String.format(sql, userid,flow_biz_view);
		
		List<VOFlowCount> lists = null;
		try {
			lists = super.getClassedAllList(sql, VOFlowCount.class);
		} catch (DangerousOperatorException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
				
		return lists;
	}
	
}
